package com.jdrx.service;

import java.io.File;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import com.jdrx.beans.dto.ConsultDTO;
import com.jdrx.exception.BizException;

/**
 * 邮件发送
 */
@Service
public class MailService {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private JavaMailSender mailSender;
	@Value("${spring.mail.from}")// 邮件发送者，公司qq企业邮箱
	private String from;
	@Value("${spring.mail.to}")// 邮件接收者，逗号分割的字符串
	private String to;
	@Value("${spring.mail.subject}")// 邮件发送主题
	private String subject;

	public void send(ConsultDTO dto) throws BizException {
		if(to == null || "".equals(to)) {
			logger.error("没有配置邮件接收人！！！");
			return;
		}
		
		// 拼接html格式邮件内容
		StringBuffer content = new StringBuffer();
		content.append("<h2>您好，这是来自九瑞水务云客户咨询相关业务的邮件，请及时处理。</h2>");
		content.append("<p>咨询主题：").append(dto.getScene()).append("</p>");
		content.append("<p>咨询问题：").append(dto.getQuestion()).append("</p>");
		content.append("<p>咨询人姓名：").append(dto.getName()).append("</p>");
		content.append("<p>咨询人电话：").append(dto.getPhone()).append("</p>");
		content.append("<p>咨询人职务：").append(dto.getJob()).append("</p>");
		content.append("<p>咨询人邮箱：").append(dto.getEmail()).append("</p>");
		content.append("<p>咨询人公司：").append(dto.getCompany()).append("</p>");
		content.append("<p>咨询人公司员工人数：").append(dto.getStuff()).append("</p>");
		content.append("<p>咨询人公司所在地址：").append(dto.getAddress()).append("</p>");
		
		String[] toList = to.split(",");
		for(String toUser : toList) {
			this.sendHtmlMail(toUser, subject, content.toString());
		}
		logger.info("邮件发送成功，接收人邮箱包括{}", to);
	}
	
	public void sendMaintenance(ConsultDTO dto) throws BizException {
		if(to == null || "".equals(to)) {
			logger.error("没有配置邮件接收人！！！");
			return;
		}
		
		// 拼接html格式邮件内容
		StringBuffer content = new StringBuffer();
		content.append("<h2>站点情况</h2>");
		content.append("<p>3天<天数<6天：古堰口左坝渗压、古堰口右坝渗压").append("</p>");
		
		String[] toList = to.split(",");
		for(String toUser : toList) {
			this.sendHtmlMail(toUser, subject, content.toString());
		}
		logger.info("邮件发送成功，接收人邮箱包括{}", to);
	}

	/**
	 * 简单文本邮件
	 * 
	 * @param to      收件人
	 * @param subject 主题
	 * @param content 内容
	 * @throws BizException 
	 */
	public void sendSimpleMail(String to, String subject, String content) throws BizException {
		try {
			// 创建SimpleMailMessage对象
			SimpleMailMessage message = new SimpleMailMessage();
			// 邮件发送人
			message.setFrom(from);
			// 邮件接收人
			message.setTo(to);
			// 邮件主题
			message.setSubject(subject);
			// 邮件内容
			message.setText(content);
			// 发送邮件
			mailSender.send(message);
			logger.info("邮件发送成功，接收人邮箱={}", to);
		} catch (Exception e) {
			logger.error("邮件发送异常！！！", e);
			throw new BizException("邮件发送失败");
		}
	}

	/**
	 * html邮件
	 * 
	 * @param to      收件人
	 * @param subject 主题
	 * @param content 内容
	 * @throws BizException 
	 */
	public void sendHtmlMail(String to, String subject, String content) throws BizException {
		// 获取MimeMessage对象
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper messageHelper;
		try {
			messageHelper = new MimeMessageHelper(message, true);
			// 邮件发送人
			messageHelper.setFrom(from);
			// 邮件接收人
			messageHelper.setTo(to);
			// 邮件主题
			message.setSubject(subject);
			// 邮件内容，html格式
			messageHelper.setText(content, true);
			// 发送
			mailSender.send(message);
			// 日志信息
			logger.info("邮件发送成功，接收人邮箱={}", to);
		} catch (MessagingException e) {
			logger.error("邮件发送异常！！！", e);
			throw new BizException("邮件发送失败");
		}
	}

	/**
	 * 带附件的邮件
	 * 
	 * @param to       收件人
	 * @param subject  主题
	 * @param content  内容
	 * @param filePath 附件
	 * @throws BizException 
	 */
	public void sendAttachmentsMail(String to, String subject, String content, String filePath) throws BizException {
		MimeMessage message = mailSender.createMimeMessage();
		try {
			MimeMessageHelper helper = new MimeMessageHelper(message, true);
			helper.setFrom(from);
			helper.setTo(to);
			helper.setSubject(subject);
			helper.setText(content, true);

			FileSystemResource file = new FileSystemResource(new File(filePath));
			String fileName = filePath.substring(filePath.lastIndexOf(File.separator));
			helper.addAttachment(fileName, file);
			mailSender.send(message);
			// 日志信息
			logger.info("邮件发送成功，接收人邮箱={}", to);
		} catch (MessagingException e) {
			logger.error("邮件发送异常！！！", e);
			throw new BizException("邮件发送失败");
		}
	}
}